package com.ip.user.service.impl;

import com.ip.user.mapper.UserMapper;
import com.ip.user.models.bo.UserBO;
import com.ip.user.models.service.UserService;
import com.ip.user.models.vo.UserVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户服务实现类
 */
@DubboService
@Service
public class UserServiceImpl implements UserService {
    
    @Autowired
    private UserMapper userMapper;
    
    @Autowired
    private PasswordEncoder passwordEncoder;
    
    @Override
    public UserVO createUser(UserBO userBO) {
        // 对密码进行加密处理
        if (userBO.getPassword() != null && !userBO.getPassword().isEmpty()) {
            String encodedPassword = passwordEncoder.encode(userBO.getPassword());
            userBO.setPassword(encodedPassword);
        }
        
        // 设置创建时间
        userBO.setCreateTime(LocalDateTime.now());
        userBO.setUpdateTime(LocalDateTime.now());
        userMapper.insertUser(userBO);
        // 创建UserVO对象并返回
        UserVO userVO = new UserVO();
        userVO.setUserId(userBO.getUserId());
        userVO.setUserName(userBO.getUserName());
        userVO.setRealName(userBO.getRealName());
        userVO.setEmail(userBO.getEmail());
        userVO.setPhone(userBO.getPhone());
        userVO.setCreateTime(userBO.getCreateTime());
        userVO.setUpdateTime(userBO.getUpdateTime());
        userVO.setUserStatus(userBO.getUserStatus());
        return userVO;
    }
    
    @Override
    public UserVO getUserById(Long userId) {
        return userMapper.selectUserById(userId);
    }
    
    @Override
    public UserVO getUserByUsername(String username) {
        return userMapper.selectUserByUsername(username);
    }
    
    @Override
    public List<UserVO> getAllUsers() {
        return userMapper.selectAllUsers();
    }
    
    @Override
    public List<UserVO> getUsersByPage(int pageNo, int pageSize, String username, Integer status) {
        int offset = (pageNo - 1) * pageSize;
        return userMapper.selectUsersByPage(offset, pageSize, username, status);
    }
    
    @Override
    public int getUserCount(String username, Integer status) {
        return userMapper.selectUserCount(username, status);
    }
    
    @Override
    public UserVO updateUser(UserBO userBO) {
        // 如果密码不为空，则对密码进行加密处理
        if (userBO.getPassword() != null && !userBO.getPassword().isEmpty()) {
            String encodedPassword = passwordEncoder.encode(userBO.getPassword());
            userBO.setPassword(encodedPassword);
        }
        
        // 设置更新时间
        userBO.setUpdateTime(LocalDateTime.now());
        userMapper.updateUser(userBO);
        // 创建UserVO对象并返回
        UserVO userVO = new UserVO();
        userVO.setUserId(userBO.getUserId());
        userVO.setUserName(userBO.getUserName());
        userVO.setRealName(userBO.getRealName());
        userVO.setEmail(userBO.getEmail());
        userVO.setPhone(userBO.getPhone());
        userVO.setUpdateTime(userBO.getUpdateTime());
        userVO.setUserStatus(userBO.getUserStatus());
        return userVO;
    }
    
    @Override
    public void updateUserStatus(Long userId, Integer status) {
        userMapper.updateUserStatus(userId, status);
    }
    
    @Override
    public void deleteUser(Long userId) {
        userMapper.deleteUser(userId);
    }
}